package cn.itrigger.dynamic.sql.select.join;

import cn.itrigger.dynamic.sql.structure.SqlTable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @author admin
 * @date 2018/9/25
 **/
public class JoinSpecification {

    private SqlTable table;
    private List<JoinCriterion> joinCriteria;
    private JoinType joinType;

    private JoinSpecification(Builder builder) {
        this.table = Objects.requireNonNull(builder.table);
        this.joinCriteria = Objects.requireNonNull(builder.joinCriteria);
        this.joinType = Objects.requireNonNull(builder.joinType);
    }

    public SqlTable getTable() {
        return table;
    }

    public JoinType getJoinType() {
        return joinType;
    }

    public <R> Stream<R> mapJoinCriteria(Function<JoinCriterion, R> mapper) {
        return joinCriteria.stream().map(mapper);
    }

    public static Builder withJoinTable(SqlTable sqlTable) {
        return new Builder().withSqlTable(sqlTable);
    }

    public static class Builder {
        private SqlTable table;
        private List<JoinCriterion> joinCriteria = new ArrayList<>();
        private JoinType joinType;

        public Builder withSqlTable(SqlTable table) {
            this.table = table;
            return this;
        }

        public Builder withJoinCriteria(List<JoinCriterion> joinCriteria) {
            this.joinCriteria.addAll(joinCriteria);
            return this;
        }

        public Builder withJoinType(JoinType joinType) {
            this.joinType = joinType;
            return this;
        }

        public JoinSpecification build() {
            return new JoinSpecification(this);
        }
    }

}
